package com.vast.ui.component.controller;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.vast.ui.component.factory.FullcolorledTableViewerFactory;
import com.vast.ui.component.factory.IoTableViewerFactory;
import com.vast.ui.component.factory.ItemTableViewerFactory;
import com.vast.ui.component.factory.LedTableViewerFactory;
import com.vast.ui.component.models.ItemArray;
import com.vast.ui.component.models.ItemPanel;
import com.vast.ui.component.socketinterface.listner.IReceiveListener;
import com.vast.ui.component.socketinterface.protocol.VaSTUIComponentProtocol;
import com.vast.ui.component.socketinterface.request.Request;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.util.ConsoleLoggerSingleton;
import com.vast.ui.component.util.Logger;
import com.vast.ui.panel.fullcolorled.Activator;
import com.vast.ui.panel.fullcolorled.preferences.PreferenceConstants;

public class TableViewerController implements IReceiveListener{
	private Display display;
	private TableViewer viewer;
	private ItemPanel   itemPanel;
	private RequestQueue sendQueue;
	private Logger     logger;
	
	public TableViewerController (Composite parent, RequestQueue sendQueue) {
		int columnNumber = Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.P_COLUMN_NUMBER);
		int lineNumber    = Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.P_LINE_NUMBER);
		int itnitialValue = Integer.decode("0x" + Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_RESET_VALUE));
		display   = parent.getDisplay();
		itemPanel = new ItemPanel();
		viewer      = FullcolorledTableViewerFactory.createTableViewer(parent, itemPanel, columnNumber, lineNumber, itnitialValue);
		//viewer    = ItemTableViewerFactory.createTableViewer(parent, itemPanel, 8, 4, 0);
		//viewer    = IoTableViewerFactory.createTableViewer(parent, itemPanel, 8, 1, 0);
		//viewer    = LedTableViewerFactory.createTableViewer(parent, itemPanel, 8, 1, 0);
		logger    = ConsoleLoggerSingleton.getInstance();
	}
	
	public void init(int column, int raw, int ResetValue) {
		itemPanel.clear();
		ItemArray itemArray = null;
		for (int i = 0; i < raw; i++){
			itemArray = new ItemArray(column, ResetValue);
			itemPanel.add(itemArray);
		}
		display.syncExec(new Runnable() {
			public void run() {
				viewer.setInput(itemPanel);
			}
		});
	}
	
	public void update(int column, int raw, int UpdatedValue) {
		ItemArray itemArray = itemPanel.getTarget(raw);
		itemArray.getItem(column).value = UpdatedValue;
		itemPanel.setTarget(raw, itemArray);
		display.syncExec(new Runnable() {
			public void run() {
				viewer.setInput(itemPanel);
			}
		});
		
	}
	
	public void add(int resetValue) {
		ItemArray itemArray = new ItemArray(itemPanel.getNumberOfColumn(), resetValue);
		itemPanel.add(itemArray);
		display.syncExec(new Runnable() {
			public void run() {
				viewer.setInput(itemPanel);
			}
		});
	}
	
	public void delete() {
		ItemArray itemArray = itemPanel.getList().get(itemPanel.getList().size() - 1);
		itemPanel.remove(itemArray);
		display.syncExec(new Runnable() {
			public void run() {
				viewer.setInput(itemPanel);
			}
		});
	}

	public void received(Request request) {
		// TODO Auto-generated method stub
		switch (request.command) {
			case VaSTUIComponentProtocol.PK_FULLCOLOR_LED_INITIALIZE: {
			int index = request.readInt();
			int value = request.readInt();
			int size  = itemPanel.getNumberOfColumn();
			int raw   = index / size;
			if((index % size) > 1) {
				raw = raw + 1;
			}
			init(size, raw, value);
			break;
			}
			case VaSTUIComponentProtocol.PK_FULLCOLOR_LED_UPDATE: {
				int index = request.readInt();
				int value = request.readInt();
				int size  = itemPanel.getNumberOfColumn();
				int column = index % size;
				int raw    = index / size;
				update(column, raw, value);
				break;
			}
		}
	}

}
